using GEM.Application.Common;
using GEM.Application.Dto.Permission;
using GEM.Application.Repositories;
using GEM.Domain.Entities.App;
using GEM.Domain.Repositories;

namespace GEM.Application.Services;

public class PermissionService : IPermissionService
{
    private readonly IRepository<AppPermission> _EfPermissionRep;
    private readonly IDapperRepository<AppPermission> _DpPermissionRep;
    public PermissionService(IRepository<AppPermission> EfPermissionRep, IDapperRepository<AppPermission> DpPermissionRep)
    {
        _EfPermissionRep = EfPermissionRep;
        _DpPermissionRep = DpPermissionRep;
    }
    public async Task<dynamic> CreatePermissionAsync(string permissionName)
    {
        var permissions = await _EfPermissionRep.GetAllAsync();
        // 检查该权限是否存在，存在则不创建以及返回信息
        var exists = permissions.Any(p => p.PermissionName == permissionName);
        if (exists)
        {
            return ApiResponse<string>.Fail(4001, $"{permissionName}权限已存在，无需多次创建!!");
        }
        var permission = new AppPermission { Id = Guid.NewGuid(), PermissionName = permissionName };
        var number = await _DpPermissionRep.CreateAsync(permission);
        if (number > 0)
        {
            return ApiResponse<string>.Success($"创建{permissionName}权限成功");
        }
        return ApiResponse<string>.Fail(4002, "创建权限失败，请重试!!");
    }

    public async Task<dynamic> DisableRoleAsync(Guid permissionId)
    {
        var permission = await _DpPermissionRep.GetByIdAsync(permissionId);
        if (permission == null)
        {
            return ApiResponse<string>.Fail(1004, "权限不存在,请确认后重试!!");
        }
        if (permission.IsDeleted == true)
        {
            return ApiResponse<string>.Fail(1004, "该权限已删除,请确认后重试!!");
        }
        if (permission.IsActived == false)
        {
            return ApiResponse<string>.Fail(1004, "权限已禁用,无需重复禁用");
        }
        permission.IsActived = false;
        permission.UpdatedAt = DateTime.Now;
        var number = await _DpPermissionRep.UpdateAsync(permission);
        if (number > 0)
        {
            return ApiResponse<string>.Success("禁用成功");
        }
        return ApiResponse<string>.Fail(1004, "禁用失败,请重试!!");
    }

    public async Task<dynamic> EnableRoleAsync(Guid permissionId)
    {
        var permission = await _DpPermissionRep.GetByIdAsync(permissionId);
        if (permission == null)
        {
            return ApiResponse<string>.Fail(1004, "权限不存在,请确认后重试!!");
        }
        if (permission.IsDeleted == true)
        {
            return ApiResponse<string>.Fail(1004, "该权限已删除,请确认后重试!!");
        }
        if (permission.IsActived == true)
        {
            return ApiResponse<string>.Fail(1004, "权限已启用,无需重复启用");
        }
        permission.IsActived = true;
        permission.UpdatedAt = DateTime.Now;
        var number = await _DpPermissionRep.UpdateAsync(permission);
        if (number > 0)
        {
            return ApiResponse<string>.Success("启用成功");
        }
        return ApiResponse<string>.Fail(1004, "启用失败,请重试!!");
    }

    public async Task<PagedResult<PermissionDto>> GetRolePagedAsync(int pageIndex, int pageSize)
    {
        var (permissions, totalCount) = await _EfPermissionRep.GetPagedAsync(pageIndex, pageSize);
        var permissionDto = permissions.Select(permission => new PermissionDto(
            permission.Id,
            permission.PermissionName,
            permission.IsActived,
            permission.IsDeleted,
            permission.CreatedAt,
            permission.UpdatedAt
        )).ToList();
        var pagedResult = new PagedResult<PermissionDto>
        {
            Items = permissionDto,
            TotalCount = totalCount,
            PageIndex = pageIndex,
            PageSize = pageSize
        };
        return pagedResult;
    }

    public async Task<dynamic> RemovePermissionAsync(Guid permissionId)
    {
        var permission = await _EfPermissionRep.GetPermissionWithRolesAsync(permissionId);
        if (permission == null)
        {
            return ApiResponse<string>.Fail(4001, "权限不存在,请确认后重试!!");
        }
        await _EfPermissionRep.DeleteAsync(permission);
        return ApiResponse<string>.Success($"{permission.PermissionName}权限删除成功");
    }
}